<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div class="search">
      <el-form ref="search" :inline="true" :model="search">
        <!-- 区域搜索 -->
        <el-form-item label="区域搜索:" size="medium">
          <el-input v-model="search.name" clearable placeholder="请输入" />
        </el-form-item>
        <el-button
          type="primary"
          class="my-primary1"
          icon="el-icon-search"
          @click="getEreaList"
        >查询</el-button>
      </el-form>
    </div>

    <!-- 表单区域 -->
    <div class="result">
      <!-- 头部 -->
      <el-row class="operation">
        <el-button
          type="primary"
          class="my-primary2"
          icon="el-icon-circle-plus-outline"
          @click="addFormDialog = true"
        >新建
        </el-button>
      </el-row>
      <!-- 表单主体 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          :index="(index) => (search.pageIndex - 1) * 10 + (index + 1)"
          label="序号"
          width="80"
        />
        <el-table-column prop="name" label="区域名称" />
        <el-table-column prop="nodeCount" label="点位数" />
        <el-table-column prop="remark" label="备注说明" />
        <el-table-column label="操作" width="200">
          <template v-slot="{ row }">
            <el-button
              type="text"
              class="el-button--primary-text"
              @click="showDetailDialog(row.id,row.name)"
            >
              查看详情
            </el-button>
            <el-button
              type="text"
              class="el-button--primary-text"
              @click="modifyErea(row )"
            >
              修改
            </el-button>
            <el-button
              type="text"
              class="el-button--primary-text del-color"
              @click="delErea(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表单底部 -->
      <div class="pagination-container">
        <el-pagination
          layout="slot,prev,next"
          :current-page.sync="search.pageIndex"
          prev-text="上一页"
          next-text="下一页"
          background
          :total="totalCount"
          :page-size="search.pageSize"
          @current-change="getEreaList"
        >
          <template order="-1">
            <span
              class="el-pagination__total"
            >共{{ totalCount }}条记录 第 {{ search.pageIndex }}/{{
              totalPage
            }}
              页</span>
          </template>
        </el-pagination>
      </div>
    </div>
    <!-- 新建区域弹窗 -->
    <el-dialog :title="`${currentId ? '编辑' :'新增'}区域`" :visible.sync="addFormDialog" width="630px">
      <el-form ref="addForm" :model="addForm" :rules="rules" label-width="140px">
        <el-form-item label="区域名称：" prop="regionName">
          <el-input
            v-model="addForm.regionName"
            autocomplete="off"
            placeholder="请输入"
            style="width: 396px"
            show-word-limit
            maxlength="15"
          />
        </el-form-item>
        <el-form-item label="备注说明：" prop="remark">
          <el-input
            v-model="addForm.remark"
            maxlength="40"
            show-word-limit
            style="width: 396px"
            type="textarea"
            :rows="3"
            placeholder="请输入备注（不超过40字）"
          />
        </el-form-item>
        <div class="dialog-footer">
          <el-row type="flex" align="middle" justify="center">
            <el-col style="text-align:center">
              <el-button
                class="my-secondary"
                @click="addFormDialog = false"
              >取 消</el-button>
              <el-button
                type="primary"
                class="my-primary2"
                style="margin-left: 34px"
                @click="saveData"
              >确 定</el-button>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog title="区域详情" :visible.sync="ereaDetailDialog" width="630px">
      <el-form label-width="140px">
        <el-form-item label="区域名称：">
          {{ currentName }}
        </el-form-item>
        <el-form-item label="包含点位：">
          <el-table
            :data="nodeList"
            style="width: 100%"
            empty-text="暂无数据"
            fit
            highlight-current-row
            :cell-style="{
              height: '44px',
              padding: '2px 0px',
              textAlign: 'left',
              color: 'rgb(102, 102, 102)',
              borderBottom: 'none',
            }"
            :header-cell-style="{
              lineHeight: 1.15,
              padding: '10px 0px 9px',
              background: 'rgb(243, 246, 251)',
              fontWeight: 500,
              textAlign: 'left',
              color: 'rgb(102, 102, 102)',
              borderBottom: 'none',
            }"
          >
            <el-table-column
              type="index"
              :index="(index) => index + 1"
              label="序号"
              width="80px"
            />
            <el-table-column prop="name" label="点位名称" />
            <el-table-column prop="vmCount" label="设备数量" />
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getEreaList, addErea, modifyErea, getEreaDetail, delErea } from '@/api/vm'
export default {
  name: 'EreaManageContainer',
  data() {
    return {
      search: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      addFormDialog: false,
      ereaDetailDialog: false,
      addForm: {
        regionName: '',
        remark: ''
      },
      nodeList: [],
      totalPage: 0,
      totalCount: 0,
      tableData: [],
      currentId: null,
      currentName: '',
      rules: {
        regionName: [{ required: true, message: '请输入', trigger: 'change' }],
        remark: [{ required: true, message: '请输入', trigger: 'change' }]
      }

    }
  },
  created() {
    this.getEreaList()
  },
  methods: {
    async showDetailDialog(id, name) {
      this.currentName = name
      console.log(id)
      const res = await getEreaDetail({ regionId: id })
      this.nodeList = res.currentPageRecords
      this.ereaDetailDialog = true
    },
    async delErea(id) {
      await delErea(id)
      this.$message.success('删除成功')
      this.getEreaList()
    },
    async modifyErea(row) {
      console.log(row)
      this.addForm.regionName = row.name
      this.addForm.remark = row.remark
      this.addFormDialog = true
    },
    async getEreaList() {
      const res = await getEreaList({ ...this.search })
      // console.log(res)
      this.totalPage = +res.totalPage
      this.totalCount = +res.totalCount
      this.tableData = res.currentPageRecords
    },
    async saveData() {
      this.$refs.addForm.validate(async pass => {
        if (!pass) return
        this.currentId
          ? await modifyErea({ ...this.addForm, id: this.currentId })
          : await addErea({ ...this.addForm })
        this.addFormDialog = false
        this.$message.success(`${this.currentId ? '编辑' : '新增'}成功`)
        this.getEreaList()
        this.$refs.addForm.resetFields()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/common.scss";
.search .el-form-item {
  margin-bottom: 0;
}

::v-deep .el-form-item__label {
  font-weight: normal;
}

.taskDetail .el-form-item {
  margin-bottom: 0;
}

</style>
